Is it OK to write code after [super dealloc]? (Objective-C)
Posted
by
Richard J. Ross III
on Stack Overflow
See other posts from Stack Overflow
or by Richard J. Ross III
Published on 2011-01-04T13:48:21Z
Indexed on
2011/01/04
13:53 UTC
Read the original article
Hit count: 228
I have a situation in my code, where I cannot clean up my classes objects without first calling [super dealloc]
. It is something like this:
// Baseclass.m
@implmentation Baseclass
...
-(void) dealloc
{
[self _removeAllData];
[aVariableThatBelongsToMe release];
[anotherVariableThatBelongsToMe release];
[super dealloc];
}
...
@end
This works great. My problem is, when I went to subclass this huge and nasty class (over 2000 lines of gross code), I ran into a problem: when I released my objects before calling [super dealloc]
I had zombies running through the code that were activated when I called the [self _removeAllData]
method.
// Subclass.m
@implementation Subclass
...
-(void) deallloc
{
[super dealloc];
[someObjectUsedInTheRemoveAllDataMethod release];
}
...
@end
This works great, and It didn't require me to refactor any code. My question Is this: Is it safe for me to do this, or should I refactor my code? Or maybe autorelease the objects?
I am programming for iPhone if that matters any.
© Stack Overflow or respective owner